package wetogether.travelinfo.control;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import wetogether.travelinfo.service.OutputTravelInfoService;
import wetogether.travelinfo.util.CommonConstants;
import wetogether.travelinfo.vo.TravelCommentVO;

public class OutputTravelInfoController extends MultiActionController{
	private OutputTravelInfoService outputTravelInfoService;
	public void setOutputTravelInfoService(OutputTravelInfoService outputTravelInfoService){
		this.outputTravelInfoService=outputTravelInfoService;
		System.out.println("OutputTravelInfoService injection....");
	}
	/**
	 *지역정보 페이지
	 */
	public ModelAndView outputTravelZone(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("travelinfo_body");
		int locationId=Integer.parseInt(request.getParameter("locationId"));
		mv.addObject("locationId",request.getParameter("locationId"));
		try {
			Map totalMap=outputTravelInfoService.getTravelZoneOutput(locationId);
			mv.addAllObjects(totalMap);
			System.out.println("zMap:"+totalMap);
		} catch (SQLException se) {
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 여행지 정보 페이지
	 */
	public ModelAndView outputTravelInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		ModelAndView mv=new ModelAndView();
		Map map=new HashMap();
		mv.setViewName("placeinfo_output");
		String place=request.getParameter("place");
		System.out.println("place:"+place);
		String page=request.getParameter("page");
		if(page==null || page==""){
			page="1";
		}
		map.put("page", page);
		map.put("place", place);
		map.put("contentpage", CommonConstants.CONTENT_NUMBER_PER_PAGE);
		int locationId=Integer.parseInt(request.getParameter("locationId"));
		System.out.println("locationId:"+locationId);
		mv.addObject("place",place);
		mv.addObject("locationId",request.getParameter("locationId"));
		try{
			// 선택한 지역에 해당하는 여행지 정보 모두 가져오기
			Map totalMap=outputTravelInfoService.getTravelInfoOutput(map);
			mv.addAllObjects(totalMap);
			System.out.println("outputTravelInfo_totalMap:"+totalMap);
			// 메뉴에 여행지 리스트를 보여주기 위해서
			List<String> infoList=outputTravelInfoService.findTravelPlaceList(locationId);
			mv.addObject("infoList",infoList);
			// 여행지에 댓글 제한 - 1. 회원만 가능 2. 한번만 입력 가능 -을 위해
			if(session.getAttribute("memberId")!=null){
				TravelCommentVO commentVO=new TravelCommentVO();
				commentVO.setPlace(place);
				commentVO.setMemberid(session.getAttribute("memberId").toString());
				TravelCommentVO cVO=outputTravelInfoService.getPlaceComment(commentVO);
				if(cVO!=null){
					mv.addObject("cflag",true);
				}else{
					mv.addObject("cflag",false);
				}
			}
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
}
